#include <cstdio>

#include <eq/configEvent.h>
#include <eq/server.h>

#include "config.h"

eqtest::Config::Config(eq::ServerPtr parent):
eq::Config(parent),
rotating(false)
{
}

bool eqtest::Config::init(const eq::uint128_t&)
{
    this->getLocalNode()->registerObject(&state);
    
    return eq::Config::init(state.getID());
}

bool eqtest::Config::exit()
{
    bool result = eq::Config::exit();
    this->getLocalNode()->deregisterObject(&state);
    return result;
}

uint32_t eqtest::Config::startFrame(const eq::uint128_t&)
{
    return eq::Config::startFrame(state.commit());
}

bool eqtest::Config::handleEvent(const eq::ConfigEvent* event)
{
    switch(event->data.type)
    {
        case eq::Event::CHANNEL_POINTER_BUTTON_PRESS:
            std::printf("Channel click at (%d, %d), button %u.\n",
                        (int)event->data.pointerButtonPress.x,
                        (int)event->data.pointerButtonPress.y,
                        (unsigned)event->data.pointerButtonPress.button);
            return false;
        case eq::Event::WINDOW_POINTER_BUTTON_PRESS:
            std::printf("Window click at (%d, %d), button %u.\n",
                        (int)event->data.pointerButtonPress.x,
                        (int)event->data.pointerButtonPress.y,
                        (unsigned)event->data.pointerButtonPress.button);
            rotating = true;
            return false;
        case eq::Event::WINDOW_POINTER_BUTTON_RELEASE:
            rotating = false;
            return false;
        case eq::Event::WINDOW_POINTER_MOTION:
            if(rotating)
            {
                state.addRotate(event->data.pointer.dy, event->data.pointer.dx);
                return true;
            }
            else
                return false;
        case eq::Event::WINDOW_RESIZE:
            std::printf("New size: (%d. %d) (%d, %d)\n",
                        event->data.resize.x,
                        event->data.resize.y,
                        event->data.resize.w,
                        event->data.resize.h);
        default:
            return eq::Config::handleEvent(event);
    }
}
